package com.sunday.common.statemachine.study.e7_Join;

import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.config.model.StateData;
import org.springframework.statemachine.config.model.StateMachineModel;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionsData;
import org.springframework.statemachine.config.model.verifier.StateMachineModelVerifier;

import java.util.Collection;

@Slf4j
public class CustomStateMachineModelVerifier implements StateMachineModelVerifier<OrderStates, OrderEvents> {

    @Override
    public void verify(StateMachineModel<OrderStates, OrderEvents> model) {
        log.info("Starting state machine model verification...");

        // 1. 确保存在初始状态 和 结束状态
        StatesData<OrderStates, OrderEvents> statesData = model.getStatesData();
        Collection<StateData<OrderStates, OrderEvents>> stateData = statesData.getStateData();
        if (!stateData.stream().map(state -> {
            log.info("State  : {}", state.getState());
            log.info("State isInitial : {}", state.isInitial());
            log.info("State isEnd : {}", state.isEnd());
            return state;
        }).allMatch(state -> state.isInitial() && state.isEnd())) {
            throw new IllegalStateException("State machine model does not have an initial state.");
        }

        // 2. 检查每个状态的有效过渡
        TransitionsData<OrderStates, OrderEvents> transitionsData = model.getTransitionsData();


//        // 1. 确保存在初始状态
//        Set<State<OrderStates, OrderEvents>> states = model.getStates();
//        State<OrderStates, OrderEvents> initialState = model.getInitialState();
//        if (initialState == null) {
//            throw new IllegalStateException("State machine model does not have an initial state.");
//        }
//        log.info("Initial state: {}", initialState.getId());

//        // 2. 检查每个状态的有效过渡
//        for (State<OrderStates, OrderEvents> state : states) {
//            List<StateMachineTransition<OrderStates, OrderEvents>> transitions = state.getTransitions();
//            if (transitions.isEmpty()) {
//                log.warn("State '{}' has no transitions.", state.getId());
//            } else {
//                log.info("State '{}' has {} transition(s).", state.getId(), transitions.size());
//            }
//
//            // 检查是否存在某个状态的转移失败（没有有效目标）
//            for (StateMachineTransition<OrderStates, OrderEvents> transition : transitions) {
//                if (transition.getTarget() == null) {
//                    throw new IllegalStateException("Transition from state " + state.getId() + " does not have a valid target.");
//                }
//            }
//        }
//
//        // 3. 检查是否存在死锁状态（没有合法的转移）
//        for (State<OrderStates, OrderEvents> state : states) {
//            if (state.getTransitions().isEmpty()) {
//                log.warn("State '{}' has no outgoing transitions, it might be a deadlock state.", state.getId());
//            }
//        }
//
//        // 4. 检查是否所有事件都有对应的转换
//        Map<OrderStates, List<StateMachineTransition<OrderStates, OrderEvents>>> transitionsMap = model.getTransitions();
//        for (Map.Entry<OrderStates, List<StateMachineTransition<OrderStates, OrderEvents>>> entry : transitionsMap.entrySet()) {
//            OrderStates state = entry.getKey();
//            List<StateMachineTransition<OrderStates, OrderEvents>> transitionsList = entry.getValue();
//            for (StateMachineTransition<OrderStates, OrderEvents> transition : transitionsList) {
//                OrderEvents event = transition.getEvent();
//                if (event == null) {
//                    throw new IllegalStateException("Transition in state " + state + " does not have a valid event.");
//                }
//                log.info("Transition event '{}' is valid for state '{}'.", event, state);
//            }
//        }
//
//        // 5. 检查是否有遗漏的目标状态
//        for (State<OrderStates, OrderEvents> state : states) {
//            List<StateMachineTransition<OrderStates, OrderEvents>> transitions = state.getTransitions();
//            for (StateMachineTransition<OrderStates, OrderEvents> transition : transitions) {
//                State<OrderStates, OrderEvents> targetState = transition.getTarget();
//                if (targetState == null) {
//                    log.error("Transition from state '{}' has no target state.", state.getId());
//                }
//            }
//        }
//
//        // 6. 打印状态机模型的所有状态和转换信息（可选的调试信息）
//        for (State<OrderStates, OrderEvents> state : states) {
//            log.info("State: {}, Transitions: {}", state.getId(), state.getTransitions());
//        }

        log.info("State machine model verification completed.");
    }
}
